{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 语言模型实现文本生成\n",
    "https://blog.csdn.net/Yellow_python/article/details/89088854"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter\n",
    "from jieba import lcut\n",
    "from random import choice"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 语料"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Loading model from cache C:\\Users\\HONGJI~1\\AppData\\Local\\Temp\\jieba.cache\n",
      "Loading model cost 0.617 seconds.\n",
      "Prefix dict has been built succesfully.\n"
     ]
    }
   ],
   "source": [
    "corpus = '''\n",
    "风儿吹过，纷扬我的思念，小雨洒落，浸润我的牵挂，雷声哄响，共鸣所有的记忆，想你了，我的朋友，无论你身处何方，都祝愿你天天开心，幸福无限，常联系！\n",
    "时光的记忆曲线，将美好串联；人生的情感轨迹，将幸福标注；生活的点点滴滴，将快乐洋溢；生命的友情之花，将情谊芬芳。朋友，惟愿你天天开心，永享幸福！\n",
    "人生有时有无奈，爱不想爱的人，走不想走的路，做不想做的事，操不想操的心。还好有你这个朋友陪在身边，让无奈的人生平添了许多的快乐和精彩。愿你一切安好！\n",
    "微笑是个搬运工，可以随时搬走压力；微笑是个维修工，可以轻松修复哀痛；微笑是个清洁工，可以认真扫除烦忧。朋友，请每天保持微笑，成为快乐的保管工，幸福的加油工！\n",
    "上班太烦，工作太乱，领导太粘，生活太酸，怎么办？别着急，周末到了，开心为你解烦，快乐为你解难，开心让你缠绵，健康与你作伴，你就乐呵乐呵着吧！\n",
    "日头不眨眼，火热在人间。褪去厚衣衫，林荫寻悠闲。清心又寡欲，坐看风云变。烦扰多少事，轻轻付笑谈。以禅养心术，处之皆安然。盛夏时分，愿君惬意！\n",
    "微微的清风吹散烦忧，暖暖的阳光蒸发惆怅；真真的祝福轻轻送达，诚诚的情谊温暖心田。朋友，愿你保重身体，健康平安，幸福快乐，顺心如意，心想事成！\n",
    "时间，忘却不了美好的回忆；岁月，冲淡不了友情的芬芳；距离，阻断不了心灵的感应；忙碌，断绝不了彼此的情谊。朋友，惟愿你时时开怀，幸福快乐每一天！\n",
    "阳光洒在心中，惊醒一帘幽梦；问候如沐清风，蓝了朗朗晴空；泛起甜甜笑容，心事一切随风。愿你开启快乐一天！\n",
    "岁月匆匆人渐老，日子天天新模样，愿你容颜天天焕发，永远美丽又俊俏，祝福你美丽的姑娘，青春永驻，白马王子天天围着你，爱情甜甜蜜蜜，生活幸福美满！\n",
    "有一种默契，叫做我挨骂你顶着；有一种和谐，叫做我坐着你站着；有一种情谊，叫做我吃肉你喝汤；有一种祝福，叫做我发短信要你开开心心！\n",
    "一周时间不算长，心里想你直痒痒，联络短信要常发，寄托情感很重要，人生短短几十载，朋友祝福像甘泉，祝你天天喜洋洋，健康平安常相伴！\n",
    "天冷了，唐僧添了一件小棉袍，悟空添了一条小棉裤，沙僧添了一个小棉帽，八戒，你那双小手，别只顾着玩手机看短信呀，记得戴上一幅小手套。\n",
    "丝丝情感，融汇真挚的元素；滴滴友情，酝酿情谊的芬芳；点点思绪，汇成牵挂的浪花；声声问候，化作无限的美好。朋友，愿你开怀，时刻如意！\n",
    "伴着清晨的阳光，伴着愉快的心情，新的一天又开始了。在这新的一天里，亲爱的朋友我又在想你，编个短信发给你，祝你健康好身体，快乐又美丽！\n",
    "秋风从南起，落叶一夜袭，风光带凉意，秋意有几许，花落又一季，韶华君应惜，朋友应相忆，殷殷祝福语，纷飞了心绪，问候暖君心，盼尔总如意！\n",
    "天热了，我托清风，为你殷勤打扇；闲暇时，我用问候，与你绵绵攀谈；烦躁了，我洒细雨，为你洗去尘世如烟。时值酷暑，愿你心情惬意，更胜从前！\n",
    "一壶老酒，散发友情的醇香；一首老歌，唱出曾经的美好；一份思念，道出朋友的牵挂；一声问候，则满载朋友的情谊。朋友，愿你安好，没事常联系！\n",
    "微微的清风吹散烦忧，暖暖的阳光蒸发惆怅；真真的祝福轻轻送达，诚诚的情谊温暖心田。朋友，愿你保重身体，健康平安，幸福快乐，顺心如意，心想事成！\n",
    "长江冲不尽思念，大海涛不尽离愁，无论天涯海角荒滩戈壁，我的心就象空中的云，云中的雨丝丝缕缕缠缠绵绵，萦绕着沐浴着你。祝福你，我的朋友幸福平安！\n",
    "风儿吹过，纷扬我的思念，小雨洒落，浸润我的牵挂，雷声哄响，共鸣所有的记忆，想你了，我的朋友，无论你身处何方，都祝愿你天天开心，幸福无限，常联系！\n",
    "天上的星星很多，可是多不过我的思念，雨中的雷声很大，可是大不过我的牵挂，爱里的誓言最真，可是真不过我的祝福，朋友，愿你天天都有好心情，事事都如意！\n",
    "我对你的思念象深谷里的幽兰，淡淡的香气笼罩着你，而祝福是无边的关注一直飘到你心底。愿我的爱陪伴你走过新的一年！\n",
    "在这美丽温馨的世界里，聆听大地的祝福，这样洋溢希望的一年，祝愿你新年快乐，生活幸福！\n",
    "新年将至，献上一份祝福。期盼：每一天，幸福之神都陪伴着你；每一刻，快乐之神都保佑着你。兔年新年快乐！\n",
    "多一份欢欣就多一份快乐就多一份如意。愿节日的欢乐，新年的快乐，永远伴随着你。\n",
    "漫天的雪花，是飞舞的音符，以思念谱成乐章，用祝福奏出期盼，带给你，欢乐的一年。\n",
    "'''.strip().split('\\n')\n",
    "corpus = [lcut(line) for line in corpus]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## N-gram建模训练"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Unigram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "unigram = Counter(word for words in corpus for word in words)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Bigram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "bigram = {w: Counter() for w in unigram.keys()}\n",
    "for words in corpus:\n",
    "    for i in range(len(words) - 1):\n",
    "        bigram[words[i]][words[i + 1]] += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Trigram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "trigram = {words[i]+words[i+1]: Counter() for words in corpus for i in range(len(words) - 1)}\n",
    "for words in corpus:\n",
    "    for i in range(len(words) - 2):\n",
    "        trigram[words[i]+words[i+1]][words[i+2]] += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文本生成"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Bigram文本生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_text_bigram(first_word, text_length=60, dof=3):\n",
    "    if first_word not in unigram:\n",
    "        first_word = choice(unigram.keys())\n",
    "    text = first_word\n",
    "    next_words = sorted(bigram[first_word], key=lambda w: bigram[first_word][w])  # 候选词\n",
    "    for _ in range(text_length):  # text_length限制文本长度\n",
    "        if not next_words:\n",
    "            break  # 没有候选词时，退出循环\n",
    "        next_word = choice(next_words[:dof])  # 选出下个词（DOF：degree of freedom）\n",
    "        text += next_word\n",
    "        next_words = sorted(bigram[next_word], key=lambda w: bigram[next_word][w])\n",
    "    print('\\033[034mBigram文本生成：\\033[0m' + text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Trigram文本生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_text_trigram(first_word, text_length=60, dof=3):\n",
    "    if first_word not in unigram:\n",
    "        first_word = choice(list(unigram))  # 第一个词\n",
    "    second_words = sorted(bigram[first_word], key=lambda w: bigram[first_word][w])  # Bigram候选词\n",
    "    second_word = choice(second_words[:dof]) if second_words else ''  # 第二个词\n",
    "    text = [first_word, second_word]\n",
    "    next_words = sorted(trigram[first_word + second_word], key=lambda w: trigram[first_word+second_word][w])  # Trigram候选词\n",
    "    for i in range(text_length):\n",
    "        if not next_words:\n",
    "            next_words = sorted(bigram[text[-1]], key=lambda w: bigram[text[-1]][w])  # Trigram没有候选词时，进行Bigram\n",
    "            if not next_words:\n",
    "                break  # Bigram没有候选词时，退出循环\n",
    "        next_word_1 = choice(next_words[:dof])\n",
    "        text.append(next_word_1)\n",
    "        next_word_2 = ''.join(text[-2:])\n",
    "        next_words = sorted(trigram[next_word_2], key=lambda w: trigram[next_word_2][w])\n",
    "    print('\\033[035mTrigram文本生成：\\033[0m' + ''.join(text))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 效果对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_text(first_word, text_length=70, dof=7):\n",
    "    generate_text_bigram(first_word, text_length, dof)\n",
    "    generate_text_trigram(first_word, text_length, dof)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[034mBigram文本生成：\u001b[0m微笑是飞舞的点点滴滴，永享幸福平安，让无奈，操不想走的情感很重要，操的情感轨迹，永享幸福平安！\n",
      "\u001b[035mTrigram文本生成：\u001b[0m微笑是个搬运工，可以随时搬走压力；微笑是个维修工，可以认真扫除烦忧。朋友，惟愿你天天开心，永享幸福！\n"
     ]
    }
   ],
   "source": [
    "generate_text('微笑')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[034mBigram文本生成：\u001b[0m阳光洒细雨，走不想做的事，请每天保持微笑是个清洁工，爱的事，走的人生短短几十载，请每天保持微笑，操不想爱不想走不想操的人，让你作伴，做不想爱不想走的情感很重要，请每天保持微笑，让你解烦，做不想操的友情\n",
      "\u001b[035mTrigram文本生成：\u001b[0m阳光洒在心中，惊醒一帘幽梦；问候如沐清风，为你解烦，快乐为你殷勤打扇；闲暇时，我用问候，化作无限的美好；一份思念，雨中的雷声很大，可是多不过我的心。还好有你这个朋友陪在身边，让无奈的人生平添了许多的快乐，永远美丽又\n"
     ]
    }
   ],
   "source": [
    "generate_text('阳光')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 深度学习实现文本生成\n",
    "https://blog.csdn.net/Yellow_python/article/details/86788587"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "from warnings import filterwarnings\n",
    "filterwarnings('ignore')  # 不打印警告\n",
    "import numpy as np\n",
    "from keras.utils import to_categorical\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Conv1D, MaxPool1D, GlobalMaxPool1D, Dense"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "window = 32  # 滑窗大小\n",
    "filters = 25  # 卷积录波器数量\n",
    "kernel_size = 5  # 卷积核大小\n",
    "times = 8  # 训练总次数\n",
    "batch_size = 512\n",
    "epochs = 200"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 语料"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "seq_chr = \"\"\"\n",
    "《长恨歌》——白居易\n",
    "汉皇重色思倾国，御宇多年求不得。杨家有女初长成，养在深闺人未识。天生丽质难自弃，一朝选在君王侧。\n",
    "回眸一笑百媚生，六宫粉黛无颜色。春寒赐浴华清池，温泉水滑洗凝脂。侍儿扶起娇无力，始是新承恩泽时。\n",
    "云鬓花颜金步摇，芙蓉帐暖度春宵。春宵苦短日高起，从此君王不早朝。承欢侍宴无闲暇，春从春游夜专夜。\n",
    "后宫佳丽三千人，三千宠爱在一身。金屋妆成娇侍夜，玉楼宴罢醉和春。姊妹弟兄皆列土，可怜光彩生门户。\n",
    "遂令天下父母心，不重生男重生女。骊宫高处入青云，仙乐风飘处处闻。缓歌慢舞凝丝竹，尽日君王看不足。\n",
    "渔阳鼙鼓动地来，惊破霓裳羽衣曲。九重城阙烟尘生，千乘万骑西南行。翠华摇摇行复止，西出都门百余里。\n",
    "六军不发无奈何，宛转蛾眉马前死。花钿委地无人收，翠翘金雀玉搔头。君王掩面救不得，回看血泪相和流。\n",
    "黄埃散漫风萧索，云栈萦纡登剑阁。峨嵋山下少人行，旌旗无光日色薄。蜀江水碧蜀山青，圣主朝朝暮暮情。\n",
    "行宫见月伤心色，夜雨闻铃肠断声。天旋地转回龙驭，到此踌躇不能去。马嵬坡下泥土中，不见玉颜空死处。\n",
    "君臣相顾尽沾衣，东望都门信马归。归来池苑皆依旧，太液芙蓉未央柳。芙蓉如面柳如眉，对此如何不泪垂。\n",
    "春风桃李花开日，秋雨梧桐叶落时。西宫南内多秋草，落叶满阶红不扫。梨园弟子白发新，椒房阿监青娥老。\n",
    "夕殿萤飞思悄然，孤灯挑尽未成眠。迟迟钟鼓初长夜，耿耿星河欲曙天。鸳鸯瓦冷霜华重，翡翠衾寒谁与共。\n",
    "悠悠生死别经年，魂魄不曾来入梦。临邛道士鸿都客，能以精诚致魂魄。为感君王辗转思，遂教方士殷勤觅。\n",
    "排空驭气奔如电，升天入地求之遍。上穷碧落下黄泉，两处茫茫皆不见。忽闻海上有仙山，山在虚无缥渺间。\n",
    "楼阁玲珑五云起，其中绰约多仙子。中有一人字太真，雪肤花貌参差是。金阙西厢叩玉扃，转教小玉报双成。\n",
    "闻道汉家天子使，九华帐里梦魂惊。揽衣推枕起徘徊，珠箔银屏迤逦开。云鬓半偏新睡觉，花冠不整下堂来。\n",
    "风吹仙袂飘飖举，犹似霓裳羽衣舞。玉容寂寞泪阑干，梨花一枝春带雨。含情凝睇谢君王，一别音容两渺茫。\n",
    "昭阳殿里恩爱绝，蓬莱宫中日月长。回头下望人寰处，不见长安见尘雾。惟将旧物表深情，钿合金钗寄将去。\n",
    "钗留一股合一扇，钗擘黄金合分钿。但教心似金钿坚，天上人间会相见。临别殷勤重寄词，词中有誓两心知。\n",
    "七月七日长生殿，夜半无人私语时。在天愿作比翼鸟，在地愿为连理枝。天长地久有时尽，此恨绵绵无绝期。\n",
    "\"\"\".replace('\\n', '').replace('《长恨歌》——白居易', '')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "语料长度 960 字汇量 467\n"
     ]
    }
   ],
   "source": [
    "len_seq = len(seq_chr)\n",
    "chr_set = set(seq_chr)  # 字库\n",
    "len_chr = len(chr_set)\n",
    "print('语料长度', len_seq, '字汇量', len_chr)\n",
    "chr2id = {c: i for i, c in enumerate(chr_set)}\n",
    "id2chr = {i: c for c, i in chr2id.items()}\n",
    "seq_id = [chr2id[c] for c in seq_chr]  # 文字序列 --> 索引序列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 输入层和标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x.shape (928, 32, 1) y.shape (928, 467)\n"
     ]
    }
   ],
   "source": [
    "reshape = lambda x: np.reshape(x, (-1, window, 1)) / len_chr\n",
    "x = [seq_id[i: i + window] for i in range(len_seq - window)]\n",
    "x = reshape(x)\n",
    "y = [seq_id[i + window] for i in range(len_seq - window)]\n",
    "y = to_categorical(y, num_classes=len_chr)\n",
    "print('x.shape', x.shape, 'y.shape', y.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 建模"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From D:\\z\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\framework\\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n"
     ]
    }
   ],
   "source": [
    "model = Sequential()\n",
    "model.add(Conv1D(filters, kernel_size * 3, activation='relu'))\n",
    "model.add(MaxPool1D())\n",
    "model.add(Conv1D(filters * 2, kernel_size, activation='relu'))\n",
    "model.add(GlobalMaxPool1D())\n",
    "model.add(Dense(len_chr, activation='softmax'))\n",
    "model.compile('adam', 'categorical_crossentropy')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 随机采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def draw_sample(predictions, temperature):\n",
    "    pred = predictions.astype('float64')  # 提高精度防报错\n",
    "    pred = np.log(pred) / temperature\n",
    "    pred = np.exp(pred)\n",
    "    pred = pred / np.sum(pred)\n",
    "    pred = np.random.multinomial(1, pred, 1)\n",
    "    return np.argmax(pred)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def predict(t, pred=None):\n",
    "    if pred is None:\n",
    "        randint = np.random.randint(len_seq - window)\n",
    "        pred = seq_id[randint: randint + window]\n",
    "    if t:\n",
    "        print('随机采样，温度：%.1f' % t)\n",
    "        sample = draw_sample\n",
    "    else:\n",
    "        print('贪婪采样')\n",
    "        sample = np.argmax\n",
    "    for _ in range(window):\n",
    "        x_pred = reshape(pred[-window:])\n",
    "        y_pred = model.predict(x_pred)[0]\n",
    "        i = sample(y_pred, t)\n",
    "        pred.append(i)\n",
    "    text = ''.join([id2chr[i] for i in pred[-window:]])\n",
    "    print('\\033[034m%s\\033[0m' % text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From D:\\z\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\ops\\math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use tf.cast instead.\n",
      "WARNING:tensorflow:From D:\\z\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\ops\\math_grad.py:102: div (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Deprecated in favor of operator or tf.math.divide.\n",
      "-------------------------------1--------------------------------\n",
      "贪婪采样\n",
      "\u001b[034m，。。。。。，，，，，，，，，，，，。。。。子，，，，，，，，，\u001b[0m\n",
      "随机采样，温度：1.0\n",
      "\u001b[034m转梦鸟不登字谢信。王差殷颜阙霜头入旌，罢人尽摇花眸芙六百落醉，驭\u001b[0m\n",
      "随机采样，温度：1.5\n",
      "\u001b[034m堂上栈气马花驭悄闲可里重六屋龙龙虚寄缥龙光秋无怜惊芙弃凝蜀士来黄\u001b[0m\n",
      "随机采样，温度：2.0\n",
      "\u001b[034m驭电孤珑百竹仙羽埃上银下金宫睡色足汉蛾渺西阑之足躇三孤回碧裳泥遍\u001b[0m\n",
      "-------------------------------2--------------------------------\n",
      "贪婪采样\n",
      "\u001b[034m搔头不头不掩面救泪得，新，花泪相相相。黄，马马，，，玉楼愿成成。\u001b[0m\n",
      "随机采样，温度：1.0\n",
      "\u001b[034m闻歌缓歌慢舞花尽死日尽监尽银相死短旧尽钿仙翠闲暇真一惊惊雪雪暮枝\u001b[0m\n",
      "随机采样，温度：1.5\n",
      "\u001b[034m。寄。一凝求睇六股军其别爱尘新时恩池多千长千皆无，夜花在私在时不\u001b[0m\n",
      "随机采样，温度：2.0\n",
      "\u001b[034m花地男君王男雨相此君王铃生生，色足爱之娥。。骊监玉伤有人，。宠在\u001b[0m\n",
      "-------------------------------3--------------------------------\n",
      "贪婪采样\n",
      "\u001b[034m，不重生男重生女。骊宫高处入青云，仙乐风飘处处闻君缓歌慢舞凝丝竹\u001b[0m\n",
      "随机采样，温度：1.0\n",
      "\u001b[034m看玉泪相和流。黄埃散漫风萧索，侍栈萦纡登剑枝。峨嵋山下少。时，无\u001b[0m\n",
      "随机采样，温度：1.5\n",
      "\u001b[034m从重游不专夜。后宫色云重千在千三芙楼不翠春爱开春云垂姊经，千复万\u001b[0m\n",
      "随机采样，温度：2.0\n",
      "\u001b[034m散。秋，人生梧桐钗入归莱相宫君土九间此寰来夜寄见上死，六。天无，\u001b[0m\n",
      "-------------------------------4--------------------------------\n",
      "贪婪采样\n",
      "\u001b[034m不扫。梨园弟子白发新，椒房阿监青娥老。夕殿萤飞思悄然，孤灯挑尽未\u001b[0m\n",
      "随机采样，温度：1.0\n",
      "\u001b[034m钿坚，天上天间会相。。临。人处心。风，，未。不堂不。春。地死。，\u001b[0m\n",
      "随机采样，温度：1.5\n",
      "\u001b[034m生丽子难仙弃，朝朝选在不重侧。回，回王月王，一遍一一上天子望。。\u001b[0m\n",
      "随机采样，温度：2.0\n",
      "\u001b[034m勤勤处时人地，下相不青心花之。春日长。。，，寰词黄金。宴翠破泥士\u001b[0m\n",
      "-------------------------------5--------------------------------\n",
      "贪婪采样\n",
      "\u001b[034m暮暮情。行宫见月伤心色，夜雨闻铃肠断声。天旋地转回龙驭，到此踌躇\u001b[0m\n",
      "随机采样，温度：1.0\n",
      "\u001b[034m无闲暇，春从春游夜专夜。后宫佳丽三千人，三千宠爱在一身。金屋妆成\u001b[0m\n",
      "随机采样，温度：1.5\n",
      "\u001b[034m天子使，九华帐里梦梦惊。揽衣推枕不徘徊夜新云起王迤。不土不宫新偏\u001b[0m\n",
      "随机采样，温度：2.0\n",
      "\u001b[034m下空驭气奔旧泪骑回天钿如一不王。，处，，尽天生朝余脂。侍有勤旧赐\u001b[0m\n",
      "-------------------------------6--------------------------------\n",
      "贪婪采样\n",
      "\u001b[034m仙乐风飘处处闻。缓歌慢舞凝丝竹，尽日君王看不足。渔阳鼙鼓动地来，\u001b[0m\n",
      "随机采样，温度：1.0\n",
      "\u001b[034m门户。遂令天下父母心，不重生男重生女。骊宫高处入山云，仙乐风飘承\u001b[0m\n",
      "随机采样，温度：1.5\n",
      "\u001b[034m悠悠生死别经年，魂魄不曾来入梦。临邛道士鸿都客，能以精诚致魂魄魂\u001b[0m\n",
      "随机采样，温度：2.0\n",
      "\u001b[034m华摇摇行复止，止出都门百。里。，军，人阳无心无无无日无无思容驭下\u001b[0m\n",
      "-------------------------------7--------------------------------\n",
      "贪婪采样\n",
      "\u001b[034m，太液芙蓉未央柳。芙蓉如面柳如眉，对此如何不泪垂。春风桃李花开日\u001b[0m\n",
      "随机采样，温度：1.0\n",
      "\u001b[034m，西出都门百余里。六军不发无奈何，宛转蛾眉马前死。花钿委地无人收\u001b[0m\n",
      "随机采样，温度：1.5\n",
      "\u001b[034m伤心一，夜雨闻铃肠断声。不旋地容不龙驭一到父踌躇头。心下。望马都\u001b[0m\n",
      "随机采样，温度：2.0\n",
      "\u001b[034m君王侧。为眸翠笑百共生宫六宫粉黛上宫回丽赐千赐池池池玉楼度楼玉爱\u001b[0m\n",
      "-------------------------------8--------------------------------\n",
      "贪婪采样\n",
      "\u001b[034m将去。钗留一股合一扇，钗擘黄金合分钿。但教心似金钿坚，天上人间会\u001b[0m\n",
      "随机采样，温度：1.0\n",
      "\u001b[034m。黄埃散漫风萧索，云栈萦纡登剑阁。峨嵋山下少人行，旌旗无光日色薄\u001b[0m\n",
      "随机采样，温度：1.5\n",
      "\u001b[034m萤飞思悄然，孤灯挑尽未成眠。迟迟钟鼓初长夜日耿耿星河欲曙天。鸳鸯\u001b[0m\n",
      "随机采样，温度：2.0\n",
      "\u001b[034m椒房阿监青娥老。夕殿萤飞思悄然时孤灯挑无不成。。，迟新鼓思长心春\u001b[0m\n",
      "----------------------------complete----------------------------\n",
      "贪婪采样\n",
      "\u001b[034m，见不见遂土遂，重。不色两可碧无回无渺，渺。渺，人茫尘尘。见城玉\u001b[0m\n",
      "随机采样，温度：1.0\n",
      "\u001b[034m，，不见月土遂，重天彩色两可到无回无君，渺茫渺无起春不起。一鬓地\u001b[0m\n",
      "随机采样，温度：1.5\n",
      "\u001b[034m，阳不。月伤遂长重，不可如夜无人此人泪人。钿天眉云似，钿。君，似\u001b[0m\n",
      "随机采样，温度：2.0\n",
      "\u001b[034m坡，遂，。，遂，马，绵。爱。两。渺铃渺铃殿茫，爱钿爱心春。。。多\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "for e in range(times):\n",
    "    model.fit(x, y, batch_size, epochs, verbose=0)\n",
    "    print(str(e + 1).center(window * 2, '-'))\n",
    "    # 训练效果展示\n",
    "    for t in (None, 1, 1.5, 2):\n",
    "        predict(t)\n",
    "print('complete'.center(window * 2, '-'))\n",
    "for t in (None, 1, 1.5, 2):\n",
    "    predict(t, seq_id[-window:])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
